<template>
  <div class="roles">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- card组件 -->
    <el-card class="box-card">
      <!-- layout组件 -->
      <el-row>
        <el-col :span="5">
          <el-button type="primary" @click="addRolesDialog = true"
            >添加角色</el-button
          >
        </el-col>
      </el-row>
      <!-- table组件 -->
      <el-table :data="rolesList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scoped">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="getRolesListById(scoped.row.id)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeRoles(scoped.row.id)"
              >删除</el-button
            >
            <el-button type="warning" icon="el-icon-setting" size="mini"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加角色弹出框 -->
    <el-dialog title="添加角色" :visible.sync="addRolesDialog" width="50%">
      <el-form
        :model="addRolesForm"
        :rules="addRolesFormRules"
        ref="addRolesForm"
        label-width="80px"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRolesForm.roleName"></el-input>
        </el-form-item>

        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addRolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRolesDialog = false">取消</el-button>
        <el-button type="primary" @click="addRoles">确定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑角色弹出框 -->
    <el-dialog title="更新角色信息" :visible.sync="editRolesDialog" width="50%">
      <el-form
        :model="editRolesForm"
        :rules="editRolesFormRules"
        ref="editRolesFormRef"
        label-width="80px"
      >
        <el-form-item label="角色名称" prop="username">
          <el-input v-model="editRolesForm.roleName"></el-input>
        </el-form-item>

        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editRolesForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editRolesDialog = false">取消</el-button>
        <el-button type="primary" @click="updateRoles">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [],
      addRolesDialog: false,
      // 添加角色数据对象
      addRolesForm: {
        roleName: "",
        roleDesc: ""
      },
      // 添加角色校验规则对象
      addRolesFormRules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "角色名称长度在 3 到 10 个字符",
            trigger: "blur"
          }
        ],
        roleDesc: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "角色描述长度在 3 到 50 个字符",
            trigger: "blur"
          }
        ]
      },
      // 控制编辑角色弹框
      editRolesDialog: false,
      // 编辑角色数据对象
      editRolesForm: {
        roleName: "",
        roleDesc: ""
      },
      // 编辑角色校验规则对象
      editRolesFormRules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "角色名称长度在 3 到 10 个字符",
            trigger: "blur"
          }
        ],
        roleDesc: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "角色描述长度在 3 到 10 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    // 获取角色列表
    async getRolesList() {
      const { data: res } = await this.$http.get("roles");
      // 如果获取失败 告诉用户失败了
      if (res.meta.status !== 200) {
        return this.$message.error("获取用户列表失败");
      }
      // 把结果给表格
      this.rolesList = res.data;
    },
    // 添加角色
    addRoles() {
      this.$refs.addRolesForm.validate(async valid => {
        if (!valid) return;
        const { data: res } = await this.$http.post("roles", this.addRolesForm);
        if (res.meta.status !== 201) {
          return this.$message.error("添加角色失败");
        }
        this.addRolesDialog = false;
        this.$message.success("添加角色成功");
        this.getRolesList();
        this.$refs.addRolesForm.resetFields();
      });
    },
    // 修改角色
    async getRolesListById(id) {
      this.editRolesDialog = true;
      const { data: res } = await this.$http.get(`roles/${id}`);
      if (res.meta.status !== 200) {
        return this.$message.error("获取角色信息失败");
      }
      this.editRolesForm = res.data;
    },
    // 更新角色信息
    updateRoles() {
      this.$refs.editRolesFormRef.validate(async valid => {
        if (!valid) return;
        // 保存数据
        const { data: res } = await this.$http.put(
          `roles/${this.editRolesForm.roleId}`,
          this.editRolesForm
        );
        if (res.meta.status !== 200) {
          return this.$message.error("角色信息修改失败");
        }
        this.editRolesDialog = false;
        this.$message.success("角色信息修改成功");
        this.getRolesList();
      });
    },
    // 删除角色信息
    removeRoles(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.delete("roles/" + id).then(() => {
            this.getRolesList();
          });
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  created: function() {
    this.getRolesList();
  }
};
</script>

<style>
.box-card {
  margin-top: 15px;
}
</style>
